#include "StdAfx.h"
#include "MyFile.h"

void MyFile::ReadFile(vector<char*>&aaaa, char *filename, AVLTree &t, TreeAVL &tree)
{
	ifstream input(filename);
	char * _str;
	t.Init(tree);
	int i=0;
	if(input.is_open())
	{
		while(input.good())
		{
			_str = new char[100];
			input>>_str;
			aaaa.push_back(_str);
			aaaa.push_back(" ");
			t.InsertNode(tree,StandardString(_str),i);
			i+=2;
		}
	}
	input.close();
}



char * MyFile::StandardString(char *&c)
{
	char *a = new char[strlen(c)];
	int j=0;
	for(int i=0; i< strlen(c);i++)
	{
		if((c[i]>=65 && c[i] <=90) ||(c[i] >=97 && c[i] <=122)||c[i] == 45 || c[i] == 39)
		{
			if(c[i] >= 65 && c[i] <= 90)
			{
				c[i] = c[i] +32;
			}
			a[j] = c[i];
			j++;
		}
		if(i == strlen(c) -1)
		{
			a[j] = '\0';
		}
	}

	return a;
}

void MyFile::ReadFileCommand(char *filename, vector<char*>&_listtring1, vector<char*>&_listtring2,TreeAVL &tree, AVLTree &t)
{
	ifstream input(filename);
	char* _str;
	int i=1;
	if(input.is_open())
	{
		while(input.good())
		{
			_str = new char[100];
			input>>_str;
			
			if(i >1)
			{
				if(i%2 == 0)
				{
					_listtring1.push_back(_str);
				}
				else
				{
					_listtring2.push_back(_str);
				}

				i++;
			}
			else
			{
				i++;
			}
			
		}
	}
	input.close();
	
	
}


void MyFile::OutputFile(AVLTree &t,vector<char*>&aaaa, TreeAVL &tree, char *filename,vector<char*>&_liststring1, vector<char*>&_liststring2)
{
	ofstream out;
	out.open(filename);
	out<<"Tu va vi tri cua no"<<endl<<endl;
	t.RNL(tree,out);
	out<<endl<<endl<<"chuoi sau khi duoc thay the la "<<endl;
	t.ReplaceString(tree,aaaa,_liststring1,_liststring2,out);

	for(int i=0;i<aaaa.size();i++)
	{
		out<<aaaa[i];
	}
}



MyFile::MyFile(void)
{
}


MyFile::~MyFile(void)
{
}
